package Java.view.game.doudizhurule;

import Java.utils.JDBC;
import Java.view.game.dian21.Room21;
import Java.view.game.douniu.douniuRoom;
import domain.Player;
import domain.User;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class MyThread extends Thread{
    Player player;
    public static User[] users=new User[3];
    public MyThread(Player player) throws IOException {
        this.player=player;
        player.reader=new BufferedReader(new InputStreamReader(player.getSocket().getInputStream()));
        player.writer=new BufferedWriter(new OutputStreamWriter(player.getSocket().getOutputStream()));
    }
    @Override
    public void run() {
        System.out.println("线程开始");
        try {
            while (true) {
                //以结尾符号$分割，获取消息
                String message= player.reader.readLine().split("\\$")[0];
                handleReceivedMessage(message);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }




    public void handleReceivedMessage(String message) throws IOException {
        System.out.println("准备处理信息"+message);
        //消息分段存入列表，并获取消息头
        String[] messageList=message.split("#");
        String head=messageList[0];
        //收到登录信息 login#[mail]#[password]$
        //开始斗地主
        if(head.equals("login1")){

            try {
                User user=new User(messageList[1]);
                Connection connection= JDBC.getConnection();
                String sql = "select * from user where username='" + user.getUsername() + "'";
                try {
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery(sql);
                    if(resultSet.next()){
                        user.setPassword(resultSet.getString("password"));
                        user.setExp(resultSet.getInt("exp"));
                        user.setHappyDou(resultSet.getInt("happyDou"));
                        for(int i=0;i<3;i++){
                            if(MyThread.users[i]==null){
                                MyThread.users[i]=user;
                                break;
                            }
                        }
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println(Arrays.toString(users));
                player.setUserName(messageList[1]);
                Server.joinRoom(player);
            } catch (IndexOutOfBoundsException e) {
                System.out.println("发送胜利消息时数组越界");
                e.printStackTrace();
            }
        }
        //开始叫地主
        else if(head.equals("callLandlord")){
            Room.selectLord(Integer.parseInt(messageList[2]),Integer.parseInt(messageList[1]));
        }
        //抢地主结束
        else if(head.equals("calllandlordfinished")){
            Room.getLandlord();
        }
        //斗地主出牌或不要
        else if(head.equals("playcard")){
            Room.times++;
            Room.checkScore(messageList[2]);
            Room.sendToOthers1(Integer.parseInt(messageList[1]),message+"$");
        }
        //斗地主结束
        else if(head.equals("victory")){
            try {
                String msg="victory#"+messageList[2]+"#"+messageList[3]+"#"+messageList[4]+"#"+messageList[5]+"$";
                Room.sendToOthers1(Integer.parseInt(messageList[1]),msg);
                Server.removeAll();
            } catch (IndexOutOfBoundsException e) {
                System.out.println("发送胜利消息时数组越界");
                Server.removeAll();
                e.printStackTrace();
            }
        }
        //开始21点
        else if(head.equals("login2")){
            try {
                User user=new User(messageList[1]);
                Connection connection= JDBC.getConnection();
                String sql = "select * from user where username='" + user.getUsername() + "'";
                try {
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery(sql);
                    if(resultSet.next()){
                        user.setPassword(resultSet.getString("password"));
                        user.setExp(resultSet.getInt("exp"));
                        user.setHappyDou(resultSet.getInt("happyDou"));
                        for(int i=0;i<3;i++){
                            if(MyThread.users[i]==null){
                                MyThread.users[i]=user;
                                break;
                            }
                        }
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println(Arrays.toString(users));
                player.setUserName(messageList[1]);
                Server.joinRoom21(player);
            } catch (IndexOutOfBoundsException e) {
                System.out.println("发送胜利消息时数组越界");
                e.printStackTrace();
            }

        }
        //21点要拍
        else if(head.equals("playcard1")){
            Room21.times++;
            Room21.sendPai(Integer.parseInt(messageList[1]),messageList[2]);//玩家ID和要不要牌
        }
        //21点游戏结束
        else if(head.equals("ok")){
            Room21.jieguo(Integer.parseInt(messageList[1]));
            Server.removeAll();
        }
        //斗牛房间
        else if(head.equals("login3")){
            try {
                User user=new User(messageList[1]);
                Connection connection= JDBC.getConnection();
                String sql = "select * from user where username='" + user.getUsername() + "'";
                try {
                    Statement statement = connection.createStatement();
                    ResultSet resultSet = statement.executeQuery(sql);
                    if(resultSet.next()){
                        user.setPassword(resultSet.getString("password"));
                        user.setExp(resultSet.getInt("exp"));
                        user.setHappyDou(resultSet.getInt("happyDou"));
                        for(int i=0;i<3;i++){
                            if(MyThread.users[i]==null){
                                MyThread.users[i]=user;
                                break;
                            }
                        }
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println(Arrays.toString(users));
                player.setUserName(messageList[1]);
                Server.joinRoom_niu(player);
            } catch (IndexOutOfBoundsException e) {
                System.out.println("发送胜利消息时数组越界");
                e.printStackTrace();
            }
        }
        //斗牛加注
        else if(head.equals("addBet")) {
            douniuRoom.addBet(Integer.parseInt(messageList[1]));
        }
        //斗牛停止加注
        else if(head.equals("stopBet")){
            douniuRoom.stopBet(Integer.parseInt(messageList[1]));
        }
        //斗牛结算
        else if(head.equals("score")) {
            douniuRoom.getScore(Integer.parseInt(messageList[1]),Integer.parseInt(messageList[2]));
        }
    }
}
